package com.control.qct.businesssteward.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.TypeStatisticAdapter;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.costom.PieChartShow;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.model.TypeStatisticModel;
import com.control.qct.businesssteward.utils.DateUtil;
import com.github.mikephil.charting.charts.PieChart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 品类统计表
 * Created by Administrator on 2017/12/26 0026.
 */

public class TypeStatisticFragment extends BaseFragment {
    @BindView(R.id.pc_type)
    PieChart mPcType;
    @BindView(R.id.ll_top)
    LinearLayout mLlTop;
    @BindView(R.id.rv_type_statistical)
    RecyclerView mRvTypeStatistical;
    @BindView(R.id.tv_name)
    TextView mTvName;

    private Map<String, String> mParams;
    private TypeStatisticAdapter mTypeStatisticAdapter;


    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_type_statistic, container, false);
    }

    @Override
    protected void initView(View view) {
        activity.setTitle("品类统计表");
        mTvName.setText("品类");
        mRvTypeStatistical.setLayoutManager(new LinearLayoutManager(activity));
        mTypeStatisticAdapter = new TypeStatisticAdapter();
        mRvTypeStatistical.setAdapter(mTypeStatisticAdapter);
        if ((mSPUtils.getObject(mNetProtocol.getUserCode() + "_store")) != null)
            getTypeStatistic(DateUtil.toTime6(System.currentTimeMillis()), ((StoreModel) mSPUtils.getObject(mNetProtocol
                    .getUserCode() + "_store")).getStoreId());
    }

    /**
     * 获取品类统计数据
     */
    public void getTypeStatistic(String date, String storeId) {
        mParams = new HashMap<>();
        mParams.put("date", date);
        mParams.put("storeId", storeId);
        mNetProtocol.getTypeStatistic(activity, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                List<TypeStatisticModel> mTypeStatisticModelList = (List<TypeStatisticModel>) baseBean.getData();
                mPcType.setVisibility(mTypeStatisticModelList.size() > 0 ? View.VISIBLE : View.GONE);
                mLlTop.setVisibility(mTypeStatisticModelList.size() > 0 ? View.VISIBLE : View.GONE);
                mTypeStatisticAdapter.setNewData(mTypeStatisticModelList);
                PieChartShow.getInstance().show(mPcType, mTypeStatisticModelList);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

}
